package com.video 
{
	import fl.video.FLVPlayback;
	import fl.video.VideoEvent;
	import fl.video.VideoScaleMode;
	import flash.display.Sprite;

	/**
	 * ...
	 * @author usual
	 */
	public class VideoPlayer extends Sprite
	{
		private var _videoPlayer:FLVPlayback;
		public var _bufferingIcone:*;
		public var hadBufferICon:Boolean=false;
		public function VideoPlayer(autoRewind:Boolean) 
		{
			
			_videoPlayer = new FLVPlayback();
			this.addChild(_videoPlayer);
			_videoPlayer.x = _videoPlayer.y = 0;
			_videoPlayer.fullScreenTakeOver = false;
			_videoPlayer.bufferTime = 3;
			_videoPlayer.scaleMode = VideoScaleMode.MAINTAIN_ASPECT_RATIO;
			_videoPlayer.autoPlay = false;
			_videoPlayer.autoRewind = autoRewind;
			_videoPlayer.getVideoPlayer(_videoPlayer.activeVideoPlayerIndex).smoothing = true;
			_videoPlayer.addEventListener(VideoEvent.READY, readyVideo);
			_videoPlayer.addEventListener(VideoEvent.COMPLETE, completeVideo);
			_videoPlayer.addEventListener(VideoEvent.BUFFERING_STATE_ENTERED, bufferingStateEntered);
			_videoPlayer.addEventListener(VideoEvent.PLAYING_STATE_ENTERED, playStateEntered);
			_videoPlayer.addEventListener(VideoEvent.AUTO_REWOUND, rewind);
			_videoPlayer.addEventListener(VideoEvent.STOPPED_STATE_ENTERED, stopped);
			_videoPlayer.addEventListener(VideoEvent.PLAYHEAD_UPDATE, playHeadUpdate);
		}
		
		private function playHeadUpdate(e:VideoEvent):void 
		{
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_PLAY_HEAD_UPDATE));
		}

		private function stopped(e:VideoEvent):void 
		{
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_STOPPED));
		}
		
		private function paused(e:VideoEvent):void 
		{
			//trace("[VideoPlayer.paused] ");
		}
		private function rewind(e:VideoEvent):void 
		{
			//trace("[VideoPlayer.rewind] ");
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_REWIND));
		}
		
		private function playStateEntered(e:VideoEvent):void 
		{
			if (_bufferingIcone)_bufferingIcone.visible = false;
		}
		
		private function bufferingStateEntered(e:VideoEvent):void 
		{
			trace("[VideoPlayer.bufferingStateEntered] ");
			if (_bufferingIcone)_bufferingIcone.visible = true;
		}
		
		private function completeVideo(e:VideoEvent):void 
		{
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_COMPLETE));
		}
		
		private function readyVideo(e:VideoEvent):void 
		{
			_videoPlayer.width = _videoPlayer.preferredWidth;
			_videoPlayer.height = _videoPlayer.preferredHeight;
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_READY));
		}
		public function isPlaying():Boolean
		{
			return _videoPlayer.playing;
		}
		public function setSeeking(num:Number):void
		{
			_videoPlayer.seek(num);
			_videoPlayer.play();
		}
		
		public function setVolume(num:Number):void
		{
			_videoPlayer.volume = num;
		}
		
		public function setSeekingPercent(num:Number):void
		{
			_videoPlayer.seekPercent(num);
			//_videoPlayer.play();
		}
		
		
		public static function makeAVideoPlayer(playerName:String = '',autoRewind:Boolean=false):VideoPlayer
		{
			var player:VideoPlayer = new VideoPlayer(autoRewind);
			player.name = playerName;
			return player;
			
		}
		public function playVideo():void
		{
			_videoPlayer.play();
		}
		public function getPlayHeadPercentage():Number
		{
			return _videoPlayer.playheadPercentage;
		}
		
		public function getPlayheadTime():Number
		{
			return _videoPlayer.playheadTime;
		}
		
		public function setPlayHeadPercentage(num:Number):void
		{
			 _videoPlayer.playheadPercentage = num;
		}
		public function setBufferTime(time:Number):void
		{
			_videoPlayer.bufferTime = time;
		}
		
		public function setSource(url:String):void
		{
			trace("[VideoPlayer.setSource] ",url);
			_videoPlayer.source = url;
		}
		
		public function destroy():void
		{
			//trace("[VideoPlayer.destroy] ",_videoPlayer.name);
			trace("[VideoPlayer.destroy] ");
			_videoPlayer.removeEventListener(VideoEvent.READY, readyVideo);
			_videoPlayer.removeEventListener(VideoEvent.COMPLETE, completeVideo);
			_videoPlayer.removeEventListener(VideoEvent.BUFFERING_STATE_ENTERED, bufferingStateEntered);
			_videoPlayer.removeEventListener(VideoEvent.PLAYING_STATE_ENTERED, playStateEntered);

			_videoPlayer.stop();
			this.removeChild(_videoPlayer);

			_videoPlayer = null;
			if (this.parent != null)this.parent.removeChild(this);
			
		}
		public function get playHeadPercentage():Number
		{
			return _videoPlayer.playheadPercentage;
		}
		
		public function get playHeadTime():Number
		{
			return _videoPlayer.playheadTime;
		}
		public function pause():void 
		{
			_videoPlayer.pause();
		}
		public function unPausePlayer():void 
		{
			_videoPlayer.play();
		}
		
		public function resize():void 
		{
			var sw:Number = stage.stageWidth
			var sh:Number = stage.stageHeight
			_videoPlayer.width = sw;
			_videoPlayer.height = sh;
			//if (_bufferingIcone)
			//{
				//_bufferingIcone.x = sw/2;
				//_bufferingIcone.y = sh/2;
			//}
		}
	}

}